﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _7DaysToDieTools.Class
{
    /// <summary>
    /// 建筑相关类
    /// </summary>
    public static class BuildAction
    {
        /// <summary>
        /// 设置游戏建筑为更多建筑
        /// </summary>
        public static void SetLocalBuildMore()
        {
            //解压本地建筑数据
            DecomBuildData();
            //替换RWG配置文件
            string CompoPath = Sinfo.Data.Build + Path.GetFileNameWithoutExtension(Sinfo.Data.File.BuildFile) + "\\";

            if (File.Exists(Sinfo.Path.Config+Sinfo.File.RWG))
            {
                File.Delete(Sinfo.Path.Config + Sinfo.File.RWG);
            }
            File.Copy(CompoPath + "\\Config\\" + Sinfo.File.RWG, Sinfo.Path.Config + Sinfo.File.RWG);

            //复制建筑数据文件
            foreach (string item in Directory.GetFiles(CompoPath + "Prefabs"))
            {
                string file = Sinfo.Path.Prefabs + new FileInfo(item).Name;
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                File.Copy(item, file);
            }
        }

        /// <summary>
        /// 设置游戏建筑为默认建筑
        /// </summary>
        public static void SetBuildDefault()
        {
            //解压本地建筑数据
            DecomBuildData();
            //释放原版rwgmixer.xml
            DecomRwgmixer();
            //默认RWG配置文件

            string CompoPath = Sinfo.Data.Build + Path.GetFileNameWithoutExtension(Sinfo.Data.File.BuildFile) + "\\";

            if (File.Exists(Sinfo.Path.Config + Sinfo.File.RWG))
            {
                File.Delete(Sinfo.Path.Config + Sinfo.File.RWG);
            }
            File.Copy(Sinfo.Data.Build + Sinfo.File.RWG, Sinfo.Path.Config + Sinfo.File.RWG);

            //删除新建筑数据文件
            foreach (string item in Directory.GetFiles(CompoPath + "Prefabs"))
            {
                string file= Sinfo.Path.Prefabs + new FileInfo(item).Name;
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
            }
        }

        public static void SetBuildNet(string BuildName)
        {
            string zipFileName = GetNetBuildFile(BuildName);
            string buildPath = Sinfo.Data.Build + Path.GetFileNameWithoutExtension(zipFileName) + "\\";
            //下载和解压建筑文件
            DDNetBuildData(BuildName);
            //RWG配置文件
            if (File.Exists(Sinfo.Path.Config + Sinfo.File.RWG))
            {
                File.Delete(Sinfo.Path.Config + Sinfo.File.RWG);
            }
            File.Copy(buildPath + "\\Config\\" + Sinfo.File.RWG, Sinfo.Path.Config + Sinfo.File.RWG);
            //建筑数据文件
            foreach (string item in Directory.GetFiles(buildPath + "Prefabs"))
            {
                string file = Sinfo.Path.Prefabs + new FileInfo(item).Name;
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                File.Copy(item, file);
            }
        }
        /// <summary>
        /// 解压本地建筑数据
        /// </summary>
        public static void DecomBuildData()
        {
            if (!Directory.Exists(Sinfo.Data.Build))
            {
                Directory.CreateDirectory(Sinfo.Data.Build);
            }
            string file = Sinfo.Data.Build + Sinfo.Data.File.BuildFile;
            //释放包
            File.WriteAllBytes(file, Files.CompoPack24);
            //解压
            string unDir = Sinfo.Data.Build + Path.GetFileNameWithoutExtension(Sinfo.Data.File.BuildFile) + "\\";
            if (Directory.Exists(unDir))
            {
                Directory.Delete(unDir, true);
            }
            using (FileStream fs = File.Open(file,FileMode.Open))
            {
                ZipAction.UnzipDatas(fs, Sinfo.Data.Build);
            }
        }

        /// <summary>
        /// 解压rwgmixer.xml
        /// </summary>
        public static void DecomRwgmixer()
        {
            if (!Directory.Exists(Sinfo.Data.Build))
            {
                Directory.CreateDirectory(Sinfo.Data.Build);
            }
            string file = Sinfo.Data.Build + Path.GetFileNameWithoutExtension(Sinfo.File.RWG) + ".zip";
            //释放包
            File.WriteAllBytes(file, Files.rwgmixer);
            //解压
            using (FileStream fs = File.Open(file, FileMode.Open))
            {
                ZipAction.UnzipDatas(fs, Sinfo.Data.Build);
            }
        }

        /// <summary>
        /// 加载建筑选择框列表
        /// </summary>
        /// <param name="cbox">建筑选择框</param>
        public static void LoadBuildList(ComboBox cbox)
        {
            cbox.Items.Clear();
            //本地
            cbox.Items.Add("[本地]默认建筑");
            cbox.Items.Add("[本地]更多建筑");
            //网络
            foreach (var item in GetNetBuildList())
            {
                cbox.Items.Add("[网络]" + item.ToString());
            }
        }

        /// <summary>
        /// 获取网上建筑列表
        /// </summary>
        /// <returns></returns>
        public static List<string> GetNetBuildList()
        {
            NetConfig.GetConfig(true);
            
            List<string> listBuilds = new List<string>();
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Sinfo.Data.BasePath + Sinfo.Net.File.Config);
            foreach (var item in xDoc.SelectSingleNode("/config/builds").ChildNodes)
            {
                XmlElement xe = item as XmlElement;
                listBuilds.Add(xe.GetAttribute("name"));
            }

            return listBuilds;
        }

        /// <summary>
        /// 获取指定建筑名称的压缩包文件包
        /// </summary>
        /// <param name="BuildName">建筑名称</param>
        /// <returns></returns>
        public static string GetNetBuildFile(string BuildName)
        {
            NetConfig.GetConfig(true);
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Sinfo.Data.BasePath + Sinfo.Net.File.Config);
            XmlElement xe = xDoc.SelectSingleNode("/config/builds/build[@name='" + BuildName + "']") as XmlElement;
            if (xe == null) return "";
            return xe.InnerText;
        }

        /// <summary>
        /// 下载和解压指定名称的建筑文件
        /// </summary>
        public static void DDNetBuildData(string BuildName)
        {
            string zipFileName = GetNetBuildFile(BuildName);
            string zipFilePath = Sinfo.Data.Build + zipFileName;
            string netFilePath = Sinfo.Net.Build + zipFileName;
            if (!Directory.Exists(Sinfo.Data.Build))
            {
                Directory.CreateDirectory(Sinfo.Data.Build);
            }
            //下载
            List<string> listBuild = new List<string>();
            listBuild.Add(netFilePath);
            new FormDown(null, listBuild).ShowDialog();
            //移到到Builds目录
            if (File.Exists(zipFilePath))
            {
                File.Delete(zipFilePath);
            }
            File.Move(Sinfo.Data.BasePath + zipFileName, zipFilePath);
            //解压
            string unPath = Sinfo.Data.Build + Path.GetFileNameWithoutExtension(zipFilePath);
            if (Directory.Exists(unPath))
            {
                Directory.Delete(unPath, true);
            }
            using(FileStream fs = File.Open(zipFilePath,FileMode.Open))
            {
                ZipAction.UnzipDatas(fs, Sinfo.Data.Build);
            }
        }

    }
}
